package com.coldchain.admin.template.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.coldchain.common.vo.OrderCountResultVo;
import com.coldchain.admin.config.entity.FeeScale;
import com.coldchain.admin.config.service.IFeeScaleService;
import com.coldchain.admin.template.entity.ExpressionTemp;
import com.coldchain.admin.template.mapper.ExpressionTempMapper;
import com.coldchain.admin.template.service.IExpressionService;
import com.coldchain.admin.user.entity.Company;
import com.coldchain.admin.user.service.ICompanyService;
import com.coldchain.common.exception.base.BusinessException;
import com.coldchain.common.util.CollectFeeUtils;
import com.fangliai.core.context.BaseContextHandler;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 系统计费模板 服务实现类
 * </p>
 *
 * @author fangliai
 * @since 2018-09-13
 */
@Service
public class ExpressionServiceImpl extends ServiceImpl<ExpressionTempMapper, ExpressionTemp> implements IExpressionService {

    @Autowired
    private IFeeScaleService feeScaleService;
    @Autowired
    private IExpressionService expressionService;
    @Autowired
    private ICompanyService companyService;
    @Override
    public ExpressionTemp getDefalut() {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq(true,"isDefalut",1);
        return getOne(queryWrapper);
    }

    @Override
    public OrderCountResultVo getOrderPriceByCarTypeAndArea(int orderKm, int delivery, String cityCode, Long carTypeId, Boolean isInter) {
        Company company=companyService.getById(BaseContextHandler.getCommpanyId());
        OrderCountResultVo orderCountResult=new OrderCountResultVo();
        if(company==null){
            throw  new BusinessException("帐号异常!");
        }
        if(company.getIsOpenFixedPrice()==1){

            orderCountResult.setOrderPrice(company.getFixedPrice().doubleValue());
            return orderCountResult;
        }
        CollectFeeUtils.Numerical numerical=getNumerical(cityCode,carTypeId,isInter);

        if(isInter){
            numerical.setExpression(
                    StringUtils.isEmpty(company.getInterCityFormula())
                            ?expressionService.getDefalut().getInterCity()
                            :company.getInterCityFormula()
            );
        }else {
            numerical.setExpression(
                    StringUtils.isEmpty(company.getWideCityFormula())
                            ?expressionService.getDefalut().getWideCity()
                            :company.getWideCityFormula()
            );
        }
        numerical.setDiscounts(company.getDiscount());
        orderCountResult.setOrderPrice(new CollectFeeUtils(numerical).compute(orderKm,delivery));
        orderCountResult.setCurrentExpression(numerical.getExpression());
        orderCountResult.setDiscounts(numerical.getDiscounts());
        orderCountResult.setExceedKm(numerical.getEffectiveKm()-orderKm);
        orderCountResult.setExceedPrice(numerical.getExceedPrice());
        orderCountResult.setIntercity(isInter);
        orderCountResult.setUsePrice(numerical.getUsePrice());
        return orderCountResult;
    }

    /**
     * 获取车型的收费标准
     * @param cityCode
     * @param carTypeId
     * @param isInter
     * @return
     * @throws BusinessException
     */
    private CollectFeeUtils.Numerical getNumerical(String cityCode, Long carTypeId, Boolean isInter)throws BusinessException {
        QueryWrapper<FeeScale> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq(true,"adecode",cityCode);
        queryWrapper.eq(true,"carTypeId",carTypeId);
        FeeScale feeScale=null;
        try {
            feeScale =feeScaleService.getOne(queryWrapper);
        }catch (Exception e){
            throw  new BusinessException("查询异常");
        }
        if(feeScale==null){
            throw  new BusinessException("该地区还未配制收费标准");
        }
        CollectFeeUtils.Numerical numerical=new CollectFeeUtils.Numerical();
        numerical.setExceedPrice(feeScale.getExceedPrice().doubleValue());
        if(isInter){
            numerical.setUsePrice(feeScale.getInterCityPrice().doubleValue());
        }else{
            numerical.setUsePrice(feeScale.getWideCityPrice().doubleValue());
        }
        numerical.setEffectiveKm(feeScale.getDefalutKm());
        return numerical;
    }
}
